<?php
class Works extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->helper('url');
        if (file_exists('application/controllers/install.php'))
            redirect('install');
        $this->load->library('session');
        $this->load->model('works_model');
        $this->load->model('college_model');
        $this->load->model('category_model');

        if (!($this->session->userdata['login'])) {
            redirect('poster_admin/login');
        }

    }

    function index() {
        $this->load->library('pagination');
        /**
         *  pagination
         */
        $config['base_url'] = base_url().'poster_admin/works/index';
        $config['per_page'] = 5;
        $config['num_links'] = 7;
        $config['uri_segment'] = '4';
        $config['use_page_numbers'] = TRUE;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['first_link'] = FALSE;
        $config['last_link'] = FALSE;
        $config['total_rows'] = $this->works_model->pagination();
        $this->pagination->initialize($config);
        // pagination end
        $data['title'] = '作品列表';
        $data['query'] = $this->works_model->getByPage(5,$this->uri->segment(4)*5 -5);
        $data['page'] = $this->pagination->create_links();
        $data['category'] = $this->category_model->getAll();

        $this->load->view('templates/poster_admin/header', $data);
        $this->load->view('poster_admin/works/index', $data);
        $this->load->view('templates/poster_admin/footer', $data);
    }


    function edit() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|psd';
        $config['encrypt_name'] = 'TRUE';

        $this->load->library('upload', $config);
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', '作品名', 'required|xss_clean');
        $this->form_validation->set_rules('realname', '真实姓名', 'required|xss_clean');
        $this->form_validation->set_rules('studentnum', '学号', 'trim|required|exact_length[12]|numeric');
        $this->form_validation->set_rules('college', '学院', 'required');
        $this->form_validation->set_rules('category', '参赛项目', 'required');
        $this->form_validation->set_rules('content', '作品介绍', 'required|xss_clean|htmlspecialchars|nl2br');

        $data['title'] = '修改作品';
        $data['query'] = $this->works_model->select();
        if (empty($data['query'])) { // if uid not existed, redirect
            $data['title'] = '出错啦';
            $data['words'] = '查询id错误';
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/404', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        } else {
            $data['college'] = $this->college_model->getAll();
            $data['category'] = $this->category_model->getAll();

            if ($this->form_validation->run() == FALSE) {
                $this->load->view('templates/poster_admin/header', $data);
                $this->load->view('poster_admin/works/edit', $data);
                $this->load->view('templates/poster_admin/footer', $data);
            } else {
                $this->upload->do_upload();
                $query = $data['query'];
                $data = $this->upload->data();

                $this->works_model->edit($data['file_name'], $query['id']);
                redirect('/poster_admin/works/', 'refresh');
            }
        }
    }

    function delete() {
        $this->works_model->delete();
        redirect('/poster_admin/works/', 'refresh');
    }

    function search() {
        $this->load->library('pagination');
        $id = $this->input->get('id');

        /**
         *  pagination
         */
        $config['base_url'] = base_url().'poster_admin/works/search';
        $config['per_page'] = 5;
        $config['num_links'] = 7;
        $config['uri_segment'] = '4';
        $config['use_page_numbers'] = TRUE;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['first_link'] = FALSE;
        $config['last_link'] = FALSE;
        $config['total_rows'] = $this->works_model->searchNum($id);

        $config['suffix'] = '?id='.$this->input->get('id');
        $config['first_url'] = base_url().'poster_admin/works/search'.'?id='.$this->input->get('id');

        $this->pagination->initialize($config);
        // pagination end
        $data['title'] = '作品列表';
        $data['check'] = $this->category_model->select($id);

        if (empty($data['check'])){  // if $id not existed, redirect
            $data['title'] = '出错啦';
            $data['words'] = '查询id错误';
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/404', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        } else {
        $data['query'] = $this->works_model->getByCategory($id, 5, $this->uri->segment(4)*5 -5);
        $data['page'] = $this->pagination->create_links();
        $data['category'] = $this->category_model->getAll();

        $this->load->view('templates/poster_admin/header', $data);
        $this->load->view('poster_admin/works/index', $data);
        $this->load->view('templates/poster_admin/footer', $data);
        }
    }

}